using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.HtmlControls;

[assembly: WebResource("SharpPieces.Web.JQuery.v1_2_6.jquery-1_2_6.js", "text/javascript")] //, PerformSubstitution = true
[assembly: WebResource("SharpPieces.Web.JQuery.v1_2_6.jquery-1_2_6_min.js", "text/javascript")] //, PerformSubstitution = true
namespace SharpPieces.Web.JQuery
{

    /// <summary>
    /// Represents the base class for SharpPieces JQuery controls.
    /// </summary>
    public abstract class BaseJQueryControl : ScriptControl
    {

        // Methods

        /// <summary>
        /// Gets the script descriptors.
        /// Use it just if you want to override the <see cref="BaseJQueryControl"/> implementation, 
        /// instead use GetJQueryScriptProperties and GetJQueryScriptEvents.
        /// </summary>
        protected override IEnumerable<ScriptDescriptor> GetScriptDescriptors()
        {
            ScriptControlDescriptor descriptor = new ScriptControlDescriptor(this.GetType().FullName, this.ClientID);

            IEnumerable<KeyValuePair<string, object>> jQueryObjectProperties = this.GetJQueryObjectProperties();
            if (null != jQueryObjectProperties)
            {
                foreach (KeyValuePair<string, object> jQueryObjectProperty in jQueryObjectProperties)
                {
                    descriptor.AddProperty(jQueryObjectProperty.Key, jQueryObjectProperty.Value);
                }
            }

            IEnumerable<KeyValuePair<string, string>> jQueryObjectEvents = this.GetJQueryObjectEvents();
            if (null != jQueryObjectEvents)
            {
                foreach (KeyValuePair<string, string> jQueryObjectEvent in jQueryObjectEvents)
                {
                    descriptor.AddEvent(jQueryObjectEvent.Key, jQueryObjectEvent.Value);
                }
            }

            yield return descriptor;
        }

        /// <summary>
        /// Gets the JQuery object properties.
        /// The key is the object property name.
        /// The value is the object property initial value.
        /// </summary>
        protected abstract IEnumerable<KeyValuePair<string, object>> GetJQueryObjectProperties();

        /// <summary>
        /// Gets the JQuery object events.
        /// The key is the object event name.
        /// The value is the object event handler.
        /// </summary>
        protected abstract IEnumerable<KeyValuePair<string, string>> GetJQueryObjectEvents();

        /// <summary>
        /// Gets the script descriptors.
        /// Use it just if you want to override the <see cref="BaseJQueryControl"/> implementation, 
        /// instead use GetJQueryScriptReferences.
        /// </summary>
        protected override IEnumerable<ScriptReference> GetScriptReferences()
        {
            if (this.EnableEmbeddedScripts)
            {
#if DEBUG
                yield return new ScriptReference(
                    this.Page.ClientScript.GetWebResourceUrl(this.GetType(), "SharpPieces.Web.JQuery.jquery.v1_2_6.jquery-1_2_6.js"), 
                    typeof(BaseJQueryControl).Assembly.FullName);
#else
                yield return new ScriptReference(
                    this.Page.ClientScript.GetWebResourceUrl(this.GetType(), "SharpPieces.Web.JQuery.jquery.v1.2.6.jquery-1.2.6.min.js"), 
                    typeof(BaseJQueryControl).Assembly.FullName);
#endif

                // add custom script references

                IEnumerable<ScriptReference> jQueryScriptReferences = this.GetJQueryScriptReferences();
                if (null != jQueryScriptReferences)
                {
                    foreach (ScriptReference jQueryScriptReference in jQueryScriptReferences)
                    {
                        yield return jQueryScriptReference;
                    }
                }
            }
        }

        /// <summary>
        /// Gets the JQuery script references.
        /// </summary>
        protected abstract IEnumerable<ScriptReference> GetJQueryScriptReferences();

        /// <summary>
        /// Raises the <see cref="E:PreRender"/> event.
        /// </summary>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            // register client script initializations

            if (this.Visible)
            {
                //IEnumerable<KeyValuePair<string, string>> clientScriptInitializations = this.GetClientScriptInitializations();
                //if (null != clientScriptInitializations)
                //{
                //    foreach (KeyValuePair<string, string> clientScriptInitialization in clientScriptInitializations)
                //    {
                //        if (!this.Page.ClientScript.IsStartupScriptRegistered(typeof(BaseJQueryControl), clientScriptInitialization.Key))
                //        {
                //            this.Page.ClientScript.RegisterStartupScript(
                //                typeof(BaseJQueryControl),
                //                clientScriptInitialization.Key,
                //                clientScriptInitialization.Value);
                //        }
                //    }
                //}

                if (!this.EnableEmbeddedStyles)
                {
                    IEnumerable<string> clientStyleReferences = this.GetClientStyleReferences();
                    if (null != clientStyleReferences)
                    {
                        foreach (string clientStyleReference in clientStyleReferences)
                        {
                            this.Page.Header.Controls.Add(new LiteralControl(clientStyleReference));
                        }
                    }
                }

            }
        }

        /// <summary>
        /// Gets the control style references.
        /// </summary>
        /// <value>The control style references.</value>
        protected abstract IEnumerable<string> GetClientStyleReferences();

        // Properties

        /// <summary>
        /// Gets or sets a value indicating whether the embedded scripts used by this control are enabled.
        /// </summary>
        /// <value><c>true</c> if the embedded scripts used by this control are enabled; otherwise, <c>false</c>.</value>
        public bool EnableEmbeddedScripts
        {
            get { return (null != this.ViewState["EnableEmbeddedScripts"]) ? (bool)this.ViewState["EnableEmbeddedScripts"] : false; }
            set { this.ViewState["EnableEmbeddedScripts"] = value; }
        }

        /// <summary>
        /// Gets or sets a value indicating whether the embedded styles used by this control are enabled.
        /// </summary>
        /// <value><c>true</c> if the embedded scripts used by this control are enabled; otherwise, <c>false</c>.</value>
        public bool EnableEmbeddedStyles
        {
            get { return (null != this.ViewState["EnableEmbeddedStyles"]) ? (bool)this.ViewState["EnableEmbeddedStyles"] : false; }
            set { this.ViewState["EnableEmbeddedStyles"] = value; }
        }

        protected void RegisterEmbeddedStylesheet(string path)
        {
            //is the file already registered?
            string filePath = Page.ClientScript.GetWebResourceUrl(this.GetType(), path);
            foreach (Control ctl in Page.Header.Controls)
            {
                if (ctl is HtmlLink && (ctl as HtmlLink).Href == filePath)
                    return;
            }

            HtmlLink css = new HtmlLink();

            css.Href = Page.ClientScript.GetWebResourceUrl(this.GetType(), path);
            css.Attributes["rel"] = "stylesheet";
            css.Attributes["type"] = "text/css";
            css.Attributes["media"] = "all";
            Page.Header.Controls.Add(css);
        }

        //        //public ScriptMode LoadScriptMode
        //        //{
        //        //    get
        //        //    {
        //        //        if (ConfigurationManager.AppSettings[LOADSCRIPTMODEGLOBAL] != null)
        //        //        {
        //        //            return (ScriptMode)Enum.Parse(typeof(ScriptMode), ConfigurationManager.AppSettings[LOADSCRIPTMODEGLOBAL], true);
        //        //        }

        //        //        return _loadScriptMode;
        //        //    }
        //        //    set { _loadScriptMode = value; }
        //        //}

    }

}
